﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace ElectronicJournal.Account
{
    public partial class ContinueRegistration : System.Web.UI.Page
    {
        protected MembershipUser _membershipUser;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                if (Request.Params["ticket"] != null)
                {
                    Guid userGuid = new Guid();
                    bool guidExtractionResult = Guid.TryParse(Request.Params["ticket"].ToString(), out userGuid);
                    if (guidExtractionResult)
                    {
                        _membershipUser = Membership.GetUser((object)userGuid);
                    }
                    if (EJournalRepository.Instance.IsSubmitRegistration(userGuid))
                    {
                        IsSubmitRegistrationPanel.Visible = true;
                        EndRegistration.Visible = false;
                        FinishRegistrationPanel.Visible = false;
                        NextRegistrationPanel.Visible = false;
                    }
                    else
                    {


                        //EJournalRepository.Instance.AllScientificFirection().ForEach(n=>ScientificDirectionDropDown.Items.Cast<>()
                        ScientificDirectionDropDown.DataSource = EJournalRepository.Instance.AllScientificFirection();
                        ScientificDirectionDropDown.DataValueField = "Id";
                        ScientificDirectionDropDown.DataTextField = "Title";
                        ScientificDirectionDropDown.DataBind();
                        FinishRegistrationPanel.Visible = false;
                    }
                }
                
            }

        }
        
        protected void NextRegistrationBtn_Click(object sender, EventArgs e)
        {
            if(!(Roles.IsUserInRole(_membershipUser.UserName, "Author") | (Roles.IsUserInRole(_membershipUser.UserName, "Reviewer"))))
            {
                Roles.AddUserToRole(_membershipUser.UserName, RoleChoiceList.SelectedValue.ToString());
           //     RoleChoiceList.ViewStateMode = System.Web.UI.ViewStateMode.Disabled;
                NextRegistrationPanel.Visible = false;
            }
        }

        protected void FinishRegistrationBtn_Click(object sender, EventArgs e)
        {
            var repository = EJournalRepository.Instance;
            if (Roles.IsUserInRole(_membershipUser.UserName, "Author"))
            {
                repository.AddAuthor(FirstName.Text, LastName.Text, ThirdName.Text,
                    _membershipUser.Email, 
                    NotifyPublicationStateByEmail.Checked, 
                    NotifyMagazineNews.Checked, 
                    PhoneNumberAuthor.Text, 
                    Guid.Parse(_membershipUser.ProviderUserKey.ToString()),
                    Guid.Parse(ScientificDirectionDropDown.SelectedValue));
            }
            else if ((Roles.IsUserInRole(_membershipUser.UserName, "Reviewer")))
            {
                repository.AddReviewer(FirstName.Text,
                    LastName.Text,
                    ThirdName.Text,
                    _membershipUser.Email,
                    PhoneNumberReviewer.Text,
                    NotifyByEmail.Checked,
                    NotifyBySms.Checked,
                    Guid.Parse(_membershipUser.ProviderUserKey.ToString()));
            }
            EndRegistration.Visible = false;
            FinishRegistrationPanel.Visible = true;
        }
    }
}